#define _CRT_SECURE_NO_WARNINGS   1

#include<iostream>
#include<vector>
using namespace std;
struct TreeNode 
{
	int val;
	struct TreeNode *left;
    struct TreeNode *right;
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
	TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
		if (inorder.empty())
			return nullptr;

		int istart = 0, iend = inorder.size() - 1, pstart = 0, pend = postorder.size() - 1;
		int index = 0;

		TreeNode* root = new TreeNode(postorder[pend]);



		for (int i = 0; i<inorder.size(); ++i)
		{
			if (postorder[pend] == inorder[i])
			{
				index = i;
				break;
			}
		}
		if (index - 1 >= istart&&pstart + index - istart - 1 >= pstart)
			root->left = buildTreecore(inorder, postorder, istart, index - 1, pstart, pstart + index - istart - 1);
		if (iend >= index + 1 && pend - 1 >= pstart + index - istart)
			root->right = buildTreecore(inorder, postorder, index + 1, iend, pstart + index - istart, pend - 1);

		return root;
	}

	TreeNode* buildTreecore(vector<int>& inorder, vector<int>& postorder, int istart, int iend, int pstart, int pend)
	{
		if (istart>iend || pstart>pend)
			return nullptr;
		TreeNode* root = new TreeNode(postorder[pend]);

		int index = 0;

		for (int i = istart; i <= iend; ++i)
		{
			if (postorder[pend] == inorder[i])
			{
				index = i;
				break;
			}
		}

		if (index - 1 >= istart&&pstart + index - istart - 1 >= pstart)
			root->left = buildTreecore(inorder, postorder, istart, index - 1, pstart, pstart + index - istart - 1);
		if (iend >= index + 1 && pend - 1 >= pstart + index - istart)
			root->right = buildTreecore(inorder, postorder, index + 1, iend, pstart + index - istart, pend - 1);

		return root;

	}
};

int main()
{

}